package com.nakamachizu.controller.mixi;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.util.StringUtil;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.nakamachizu.model.LandMarkData;
import com.nakamachizu.model.MapData;
import com.nakamachizu.model.MarkData;
import com.nakamachizu.model.MemberData;
import com.nakamachizu.model.session.MixiSessionData;
import com.nakamachizu.service.MapDataService;
import com.nakamachizu.util.ErrorsUtil;

public class DeleteController extends Controller {

    private static final Logger log = Logger.getLogger(DeleteController.class.getName());
    
    @Override
    public Navigation run() throws Exception {

        MapDataService mds = new MapDataService();
        String sid = requestScope("sid");
        String keyStr = requestScope("id");
        
        if(!StringUtil.isEmpty(sid) && !StringUtil.isEmpty(keyStr)){
            MixiSessionData session = mds.getMixiSessionData(sid);
            Key key = KeyFactory.stringToKey(keyStr);
            MarkData md = Datastore.get(MarkData.class, key);
            if(md != null){
                MapData map = md.getMapDataRef().getModel();
                if(session.isOwner() && map.getAdminMailAddress().equals(session.getAdminMailAddress())){
                    if(LandMarkData.class.getSimpleName().equals(md.getMarkType())){
                        List<LandMarkData>lList = map.getLandMarkDataListRef().getModelList();
                        List<LandMarkData>newList = new ArrayList<LandMarkData>();
                        for(LandMarkData data : lList){
                            if(!KeyFactory.keyToString(key).equals(KeyFactory.keyToString(data.getKey()))){
                                newList.add(data);
                            }
                        }
                        newList = mds.allocateLandMark(newList);
                        Datastore.delete(key);
                        for(LandMarkData data : newList){
                            Datastore.put(data);
                        }
                    }else if(MemberData.class.getSimpleName().equals(md.getMarkType())){
                        MemberData mem = (MemberData)md;
                        List<MemberData>mList = map.getMemberDataListRef().getModelList();
                        List<MemberData>newList = new ArrayList<MemberData>();
                        for(MemberData data : mList){
                            if(!KeyFactory.keyToString(key).equals(KeyFactory.keyToString(data.getKey()))){
                                newList.add(data);
                            }
                        }
             
                        List<Key>lline = mds.getDeleteLineData(map, mem.getUid());
                        if(lline != null){
                            for(Key lineKey : lline){
                                Datastore.delete(lineKey);
                            }
                        }
                        
                        newList = mds.allocateMemberData(newList);
                        Datastore.delete(key);
                        for(MemberData data : newList){
                            Datastore.put(data);
                        }

                    }

                }
            }
        }
        
        
        return redirect("/mixi/view?sid=" + sid);
    }
    
    @Override
    protected Navigation handleError(Throwable error) {
        error.printStackTrace();
        log.severe(ErrorsUtil.stackTrace(error));
        return forward("/jsp/pc/mixi/error.jsp");
    }
}
